﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MyAuthCore.Entity;
using MyAuthCore.Entity.Beans;
using MyAuthCore.Model.Beans;

namespace MyAuthCore.Api.Controllers
{
    public class UserController : Controller
    {
        public JsonResult GetList(int pageIndex, int pageSize)
        {
            ResponseResult result = new ResponseResult();
            int totalCount = 0;
            List<User> list = SqlSugarHelper.DB.Queryable<User>().OrderBy(it => it.Id).ToPageList(pageIndex, pageSize, ref totalCount);
            if (totalCount <= 0)
            {
                result.Code = StateCode.Error;
                result.Message = "查询失败";
                return Json(result);
            }
            Page<User> page = new Page<User>();
            page.PageIndex = pageIndex;
            page.PageSize = pageSize;
            page.TotalCount = totalCount;
            page.List = list;
            return Json(page);
        }
        public JsonResult Add(User user)
        {
            ResponseResult result = new ResponseResult();
            result.Code = StateCode.Error;
            if (user == null)
            {
                result.Message = "参数错误！";
                return Json(result);
            }
            if (string.IsNullOrEmpty(user.Account))
            {
                result.Message = "账号不能为空！";
                return Json(result);
            }
            if (string.IsNullOrEmpty(user.Password))
            {
                result.Message = "密码不能为空！";
                return Json(result);
            }
            if (string.IsNullOrEmpty(user.Name))
            {
                result.Message = "昵称不能为空！";
                return Json(result);
            }
            var first = SqlSugarHelper.DB.Queryable<User>().Where(it => it.Account == user.Account).First();
            if (first != null)
            {
                result.Message = "该账号已被注册！";
                return Json(result);
            }
            var ret = SqlSugarHelper.DB.Insertable<User>(user).ExecuteCommand();
            if (ret <= 0)
            {
                result.Code = StateCode.Error;
                result.Message = "添加用户失败！";
                return Json(result);
            }
            result.Code = StateCode.Success;
            return Json(result);
        }
        public JsonResult Delete(User user)
        {
            ResponseResult result = new ResponseResult();
            result.Code = StateCode.Error;
            if (user == null)
            {
                result.Message = "参数错误！";
                return Json(result);
            }
            if (string.IsNullOrEmpty(user.Id))
            {
                result.Message = "用户Id不能为空！";
                return Json(result);
            }
            var ret = SqlSugarHelper.DB.Deleteable(user).ExecuteCommand();
            if (ret <= 0)
            {
                result.Code = StateCode.Error;
                result.Message = "删除用户失败！";
                return Json(result);
            }
            result.Code = StateCode.Success;
            return Json(result);
        }
    }
}